package com.hqyj.logmanagement.controller;


import com.hqyj.logmanagement.pojo.Registered;
import com.hqyj.logmanagement.service.IRegisteredService;
import com.hqyj.utile.MdFive;
import com.hqyj.utile.PojoUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 注册 前端控制器
 * </p>
 *
 * @author rock
 * @since 2021-11-30
 */
@Controller
@RequestMapping("/registered")
public class RegisteredController {
    @Resource
    IRegisteredService iRegisteredService;

    @GetMapping("register")
    public String register(){
        return "register";
    }

    @PostMapping("register")
    @ResponseBody
    public String add(Registered registered){
        boolean save=false;
        if(registered!=null&&isCanRegister(registered)){
            MdFive mdFive = new MdFive();
            registered.setSalt(registered.getUsername());
            registered.setState("0");
            registered.setPassword(mdFive.encrypt(registered.getPassword(),registered.getUsername()));
            save = iRegisteredService.save(registered);
        }
        if(save){
            return "succeed";
        }
        return "fail";
    }

    private Boolean isCanRegister(Registered registered){
        String name = registered.getName();
        String sex = registered.getSex();
        String address = registered.getAddress();
        String username = registered.getUsername();
        String password = registered.getPassword();
        String email = registered.getEmail();
        String tel = registered.getTel();
        return PojoUtil.notNull(name,sex,address,username,password,
                email,tel);
    }


}

